<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class createsurvey extends CI_Controller {
	public function ajaxCreatePage()
	{
		$html = createPage(array());
		echo json_encode(array('html' => $html));
	}
	public function ajaxInsertQuestion(){
		$surveyId = $_POST['projectId'];
		$questionType = $_POST['questionType'];
		$questionTitle = $_POST['title']; 
		$questionContent = $_POST['ans'];
		$exit = $_POST['exit'];
		
		$countQ = $this->survey_model->countQuesByIdSur($surveyId);
		$this->survey_model->insertQuestionSurvey($surveyId,$countQ);
		$t = $this->survey_model->insertQuestion($countQ,$questionTitle,$questionContent,$questionType);
		$enc = array(array('exit'=>$exit,'surId'=>$surveyId,'path'=>site_url()));
		echo json($enc);
		//echo $exit;
	}

	
	public function ajaxDelProject(){
		$id = $_POST['id']; 
		if($this->survey_model->delProject($id)){
			echo '1';
		}
	
	}
	public function ajaxEditProject(){
		$id = $_POST['idProject']; 
		$n = $_POST['nameProject'];
		$this->survey_model->editProject($id,$n);
		$rs = $this->survey_model->getInfoByProId($id);
		echo json($rs);
	}
	
	public function ajaxDelQuestion(){
		$idQues = $_POST['idDel'];
		$idPro = $_POST['idProject'];
		$this->survey_model->delQuestion($idQues,$idPro);
		echo $idQues;
	}
	public function ajaxShowQuestion(){
		$id = $_POST['idProject'];
		$rs = $this->survey_model->getInfoQuesByProId($id);
		echo json($rs);
		//echo $rs;
	}
	public function ajaxSortQuestion(){
		$reSort = $_POST['sort'];
		$listingCounter = 1;
		foreach ($reSort as $recordIDValue) {
			
			$query = "UPDATE ksv_questions SET sort = " . $listingCounter . " WHERE questionOfProject = " . $recordIDValue;
			mysql_query($query) or die('Error, insert query failed');
			$listingCounter = $listingCounter + 1;	
		}
		//print_r($reSort);
	}
	//edit question
	public function ajaxShowEditQuestion(){
		$id = $_POST['idQuestion'];
		$rs = $this->survey_model->showQuestionById($id);
		echo json($rs);
	}
	//Update content of question
	public function ajaxUpadeContentQuestion(){
		$id = $_POST['idQuestion'];
		$title = $_POST['title'];
		$content = $_POST['ans'];
		//$ran = 
		
		$this->survey_model->updateContentQuestion($id,$title,$content);
		//echo $exit;
	}
	//Del content of question
	public function ajaxDelContentQuestion(){
		$id = $_POST['idQuestion'];
		$pos = $_POST['pos'];
		$q = $this->survey_model->getQuestionById($id);
		$exp = explode(',', $q[0]['questionContent']);
		//print_r($exp);
		//echo $pos; echo count($exp);
		$newStr = '';
		foreach ($exp as $key => $value) {
			if($key != $pos){
				//if(($pos < count($exp)-1) and ($value != '')){
					$newStr .= $value.',';
				//}else{
					//$newStr .= $value;
				//}
			}
		}
		$this->survey_model->updateContentQuestion($id,$newStr);
		$arr = array(array('id' => $id,'pos' => $pos ));
		echo json($arr);
	}
	public function create()
	{
		$this->template->page_title('Tạo khảo sát');
		$this->template->meta_description('Phần này mô tả sau');
		$this->template->meta_keywords('khảo sát, khảo sát trực tuyến, điều tra, survey');
	
		if($_POST){
			$arr = array(
			'theme_id' => '1',
			'user_id' => $this->session->userdata('user_id'),
			'type' => '1',
			'survey_name' => trim($this->input->post('projectName')),
			'created_time' => date("d-m-Y, g:i a"),
			'description' => ''
			);
			$id = $this->survey_model->insertProject($arr);

			$this->survey_model->updateId($id);
			$projectName = trim($this->input->post('projectName'));
			$this->session->set_userdata('projectName',$projectName);
			//success_message('Tạo khảo sát thành công!');
			redirect('tao-cau-hoi-khao-sat/'.$id); 
		}else{
			$data['surveyId'] = '';
			$this->template->build_page('member/survey/createsurvey', $data);
		}
	}

	//edit survey
	public function editSurvey($id){
		$data['surveyId'] = '';
		$this->template->page_title('Sửa khảo sát');
		$this->template->meta_description('Phần này mô tả sau');
		$this->template->meta_keywords('khảo sát, khảo sát trực tuyến, điều tra, survey, sửa khảo sát');
		$data['survey'] = $this->survey_model->getInfoByProId($id);
		if($_POST){
			$s = trim($this->input->post('projectName'));
			$this->survey_model->editProject($id,$s);
			redirect('tao-cau-hoi-khao-sat/'.$id); 
		}else{
			$this->template->build_page('member/survey/editsurvey',$data);
		}
		
	}

	public function createquestion(){
		$this->template->page_title('Tạo câu hỏi khảo sát');
		$this->template->meta_description('Phần này mô tả sau');
		$this->template->meta_keywords('khảo sát, khảo sát trực tuyến, điều tra, survey');
		//if(isLogged()){
			$id = $this->uri->segment(2);
			$idDrop = $this->input->post('idDrop');
			$idSurvey = $this->input->post('idSurvey');
			if($idDrop == '') {
				$data['projectName'] = $this->survey_model->getInfoByProId($id);
				$data['surveyId'] = $data['projectName'][0]['survey_id'];
				$this->template->build_page('member/survey/createquestion',$data);
			}else {
				echo $idDrop;
				// $this->survey_model->insertQuestionSurvey($idSurvey,$idDrop);
				// $this->survey_model->insertQuestion($id);
			}
		//}else{
			//redirect('error/restrict');
		//}


	}

	function createinput($idDrop, $idSurvey) {
		$data['idDrop'] = $idDrop;
		$data['surveyId'] = $idSurvey;
		$this->template->build_page('member/survey/createinput',$data);
	}


	public function addquestion($projectId,$questionId){
		// $data['idDrop'] = $questionId;
		// $data['surveyId'] = $projectId;
		$this->load->helper('edit_question');
		$this->template->page_title('Sửa câu hỏi khảo sát');
		$this->template->meta_description('Phần này mô tả sau');
		$this->template->meta_keywords('khảo sát, khảo sát trực tuyến, điều tra, survey');
		$projectId = $this->uri->segment(3);
		$data['question'] = $this->survey_model->getInfoByProId($projectId);
		$data['surveyId'] = '';
		$this->template->build_page('member/survey/addquestion',$data);
	}
	public function editquestion($projectId,$questionId){
		$this->load->helper('edit_question');
		$this->template->page_title('Sửa câu hỏi khảo sát');
		$this->template->meta_description('Phần này mô tả sau');
		$this->template->meta_keywords('khảo sát, khảo sát trực tuyến, điều tra, survey');
		$projectId = $this->uri->segment(3);
		$data['question'] = $this->survey_model->getInfoByProId($projectId);
		$data['oneQuestion'] = $this->survey_model->getInfoOneQuesByProId($projectId,$questionId);
		//print_r($data['oneQuestion']);die();
		$data['surveyId'] = '';
		$this->template->build_page('member/survey/editquestion',$data);
	}
	public function review($projectId){
		$this->template->page_title('Xem trước khảo sát');
		$this->template->meta_description('Phần này mô tả sau');
		$this->template->meta_keywords('khảo sát, khảo sát trực tuyến, điều tra, survey');
		//if(isLogged()){
			$data['allTheme'] = $this->theme_model->getAllTheme();
			$userId = $this->session->userdata('user_id');
			$themeId = $this->survey_model->getInfoByProId($projectId);
			$nameTemplate = $this->theme_model->getOneTheme($themeId[0]['theme_id']);
			$data['template'] = 'templates/page/tem_survey/default';
			$data['nameTemplate'] = $nameTemplate[0]['file_name'];
			$data['surveyId'] = $themeId[0]['survey_id'];
			$data['currentSurvey'] = $themeId;
			$data['nameClass'] = 'review';
			$data['curentCustomTheme'] = $this->theme_model->getOneCustomTheme($projectId, $themeId[0]['theme_id'],$userId);
			$this->template->build_page('member/review', $data);
		// }else{
		// 	redirect('error/restrict');
		// }
	}

	function uploadImageTheme() {
		$upload_data = array();
		$config['upload_path'] = 'media/gallery/';
		$config['allowed_types'] = 'jpeg|jpg|png|gif';
		$this->load->library('upload', $config);
    	$this->upload->initialize($config);
    	var_dump($this->upload->do_upload());
    	if (!$this->upload->do_upload('fileImagesCustomTheme')) {
			echo $this->upload->display_errors();

		} else {
      		$upload_data = $this->upload->data();
      		// $picTheme = base_url().'media/pic_theme/'.$upload_data['file_name'];
      	}
	}

	function updateOptions() {
		$dataCustomTheme = array(
							'survey_id' => $this->input->post('idSurvey'),
							'theme_id' => $this->input->post('idTheme'),
							'user_id' => $this->input->post('idUser'),
							'base_font' => $this->input->post('fontBase'),
							'font_style' => $this->input->post('styleFont'),
							'width_option' => $this->input->post('widthBase'),
							'px' => $this->input->post('px'),
							'auto_width' => $this->input->post('checkboxWidth'),
							'survey_title' => $this->input->post('rdSurveyTitle'),
							'page_title' => $this->input->post('rdPageTitle'),
							'page_description' => $this->input->post('rdPageDes'),
							'question_numbers' => $this->input->post('rdQuestionNumbers'),
							'back_button' => $this->input->post('rdBackButton'),
							'progress_bar' => $this->input->post('rdProgressBar'),
							'powered_by' => $this->input->post('rdPower')
							);
		$this->theme_model->insertCustomOptionsTheme($dataCustomTheme);
		$arrCustomTheme = $this->theme_model->getOneCustomTheme($this->input->post('idSurvey'), $this->input->post('idTheme'),$this->input->post('idUser'));
		echo json_encode($arrCustomTheme);
	}

	function updateColors() {

		$dataColor = array(
						'survey_id' => $this->input->post('idSurvey'),
						'theme_id' => $this->input->post('idTheme'),
						'user_id' => $this->input->post('idUser'),
						'bg_body' => $this->input->post('clTxtBody'),
						'page_bg_color' => $this->input->post('clTxtPage'),
						'page_title_color' => $this->input->post('clTxtPageTitle'),
						'page_text_color' => $this->input->post('clTxtPageText'),
						'header_bg_color' => $this->input->post('clTxtHeader'),
						'header_text_color' => $this->input->post('clTxtHeaderText'),
						'question_bg_color' => $this->input->post('clTxtQuestion'),
						'question_title_color' => $this->input->post('clTxtQuestionTitle'),
						'progress_bar_color' => $this->input->post('clTxtProBar')
						);
		$this->theme_model->insertCustomColorsTheme($dataColor);
		$arrCustomTheme = $this->theme_model->getOneCustomTheme($this->input->post('idSurvey'), $this->input->post('idTheme'),$this->input->post('idUser'));
		echo json_encode($arrCustomTheme);
	}

	function insertTheme() {
		$idSurvey = $this->input->post('idSurvey');
		$idUser = $this->input->post('idUser');
		$dataTheme = array(
						'survey_id' => $this->input->post('idSurvey'),
						'theme_id' => $this->input->post('idTheme'),
						'user_id' => $this->input->post('idUser')
						);
		$dataSurvey = array(
						'theme_id' => $this->input->post('idTheme')
						);
		$this->theme_model->insertCustomTheme($dataTheme);
		$this->survey_model->updateSurvey($idSurvey, $idUser, $dataSurvey);
	}
}